import java.awt.Color;

import info.gridworld.grid.*;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;



public class Jumper extends Bug {

	public Jumper() {
		setColor(Color.RED);
	}

	public Jumper(Color jumperColor) {
		setColor(jumperColor);
	}

	public void move() {
		Grid<Actor> gr = getGrid();
		if (gr == null) {
			return;
		}
		Location loc = getLocation();
		Location next = loc.getAdjacentLocation(getDirection());
		if (gr.isValid(next)) {
			// 先前跳跃一个位置
			next = next.getAdjacentLocation(getDirection());
			if (gr.isValid(next)) {
				moveTo(next);
			} else {
				removeSelfFromGrid();
			}
		} else {
			removeSelfFromGrid();
		}

	}

	public boolean canMove() {
		Grid<Actor> gr = getGrid();
		if (gr == null) {
			return false;
		}
		Location loc = getLocation();
		Location next = loc.getAdjacentLocation(getDirection());
		next = next.getAdjacentLocation(getDirection());
		if (!gr.isValid(next)) {
			return false;
		}
		Actor neighbor = gr.get(next);
		
		// 跳跃到的位置是否存在对象
		return (neighbor == null);
	}

}